package be.bonamis.umpire.ui.client.mvp.impl;

import be.bonamis.umpire.ui.client.mvp.LoginView;
import be.bonamis.umpire.ui.client.resources.Resources;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.inject.Inject;

public class LoginViewImpl extends Composite implements LoginView {

	private Presenter presenter;

	interface LoginViewImplUiBinder extends UiBinder<HTMLPanel, LoginViewImpl> {
	}
	
	@UiField
	HasText login;
	
	@UiField
	HasText password;
	
	@UiField
	Button loginButton;
	
	@UiField   
    RadioButton fr;
 
    @UiField    
    RadioButton nl;

	@Inject
	public LoginViewImpl(LoginViewImplUiBinder uiBinder, final Resources resources) {
		initWidget(uiBinder.createAndBindUi(this));
		resources.css().ensureInjected();
		postInit();
	}

	private void postInit() {
//		login.setText("broche");
//		password.setText("11");
		String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
		if (currentLocale.equals("default")) {
			currentLocale = "fr";
		}
		String[] localeNames = LocaleInfo.getAvailableLocaleNames();
		for (String localeName : localeNames) {
			if (!localeName.equals("default")) {
//				String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
//				languageSelector.addItem(nativeName, localeName);
//				if (localeName.equals(currentLocale)) {
//					languageSelector.setSelectedIndex(languageSelector.getItemCount() - 1);
//				}
			}
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public Presenter getPresenter() {
		return presenter;
	}

	@Override
	public HasText getLogin() {
		return login;
	}

	@Override
	public HasText getPassword() {
		return password;
	}

	@Override
	public RadioButton getFr() {
		return fr;
	}

	@Override
	public RadioButton getNl() {
		return nl;
	}
	
	@UiHandler("loginButton")
	public void onClickLogin(ClickEvent event) {
		presenter.loginButtonClicked();
	}
}
